define([
	'jquery',
	'backbone',
	'swiper',
	'views/MainView',
	'views/ParksListView',
	'views/ParksListHeaderView',
	'views/ParksListItemView',
	'views/ParkItemHeaderView',
	'views/ParkItemView'
	], function( $, Backbone, bxslider, 
		MainView, 
		ParksListView, 
		ParksListHeaderView, 
		ParksListItemView, 
		ParkItemHeaderView,
		ParkItemView ) {

	var Main = Backbone.Router.extend({

		offView: function(view){
			if (this[view]) {
				this[view].undelegateEvents();
				this[view].off();
				this[view] = null;
			}
		},

		setApp: function() {
			App.eventHandler = Backbone.Events;

			App.navigate = function (fragment) {
				Backbone.history.navigate(fragment, {trigger: true});
				return this;
		    }

		    return App;
		},

		getMainView: function() {
			this.setApp(App);

			this.offView('mainView');

			this.mainView = new MainView();
		},

		getParksListView: function() {
			this.getMainView();

			this.offView('parksListHeaderView');
			this.offView('parksListView');

			this.parksListHeaderView = new ParksListHeaderView({
				$parent: '#view-header'
			});

			this.parksListView = new ParksListView({
				$parent: '#view-content'
			});
			this.parksListView.render();
		},

		getParkItemView: function( parkId ) {
			this.getMainView();

			this.offView('parkItemHeaderView');
			this.offView('parkItemView');

			this.parkItemHeaderView = new ParkItemHeaderView({
				$parent: '#view-header'
			});
			this.parkItemHeaderView.render();

			this.parkItemView = new ParkItemView({
				$parent: '#view-content',
				parkId: parkId
			});
		}
	});

	var getAppEvents = function(){

		_.extend(app.eventHandler, Backbone.Events);

		app.navigate = function (fragment) {
			Backbone.history.navigate(fragment, {trigger: true});
			return this;
	    }

	    return app;
	};

	return Main;
});
